<?php

/**
 * Description of ViewArticle
 *
 * @author Ian Clarence <ian.clarence@gmail.com>
 */
abstract class ViewArticle extends ViewHtmlElement {

    /**
     * The constructor.
     *
     * @param array $fields
     */
    public function __construct(array $fields = array()) {

        $fields['tag'] = 'div';
        $fields['element_id'] = $fields['panel']['article']['name'] . '_article';
        $articleTypes = ViewUtils::getArticleTypes();
        if (is_numeric($fields['panel']['article']['article_type'])) {
            $fields['class_array'] = array(
                'article',
                $articleTypes[$fields['panel']['article']['article_type']]
            );
        }

        parent::__construct($fields);

        /**
         * Debugging mode.
         */
        if (DEBUGGING_MODE == ON) {
            $articleSummary = ViewUtils::viewSummary($this->fields['panel']['article']['id'], $this->fields['panel']['article']['name']);
            $this->addInnerHtml($articleSummary);
        }
    }
    
    /**
     * Getting the URL of this page.
     * 
     * @return string
     */
    protected function getPageUrl() {
        return $this->fields['page']['full_url'];
    }
    
    /**
     * Getting the URL of the resource.
     * 
     * @return string
     */
    protected function getResource() {
        $protocol = $this->fields['page']['is_secure'] ? 'https://' : 'http://';
        return $protocol . $this->fields['page']['resource'];        
    }
    
    /**
     * Instance of an HTML form.
     * 
     * @param array $htmlForm
     * @return mixed
     */
    protected function getHtmlFormInstance($htmlForm) {
        return NULL;
    }
    
    /**
     * View fields to be passed to HTML form view object.
     * 
     * @param array $htmlForm
     * @return array
     */
    protected function getHtmlFormFields($htmlForm) {
        $output = $this->fields;
        unset($output['panel']['articles']['html_forms']);
        $output['panel']['article']['html_forms'] = $htmlForm;
        return $output;
    }
    
    /**
     * The navigation list: a list of links in an unordered list.
     * @param string $name 
     */
    protected function navigationList($name) {
        if (count($this->fields[$name]) > 0) {
            $ul = ViewHtmlFormElement::elementInit('ul');
            foreach ($this->fields[$name] as $key => $value) {
                $description = isset($value['description']) ? $value['description'] : $value['name'];
                $a = ViewHtmlFormElement::elementInit('a', $description, 'href', $value['full_url']);
                $a->setAttribute('title', $value['name']);
                $li = $a->encapsulate('li');
                $ul->addInnerHtml($li);
            }
            $this->addInnerHtml($ul);
        }
    }
    
    /**
     * The final rendering. 
     */
    public function make() {
        
        /**
         * Heading. 
         */
        if (isset($this->fields['panel']['article']['heading_text']) && $this->fields['panel']['article']['heading_text'] != '') {
            $this->addH1($this->fields['panel']['article']['heading_text']);
        }
        
        /**
         * Media. 
         */
        $mediumTypes = ViewUtils::getMediumTypes();
        foreach($this->fields['panel']['article']['media'] as $medium) {
            switch($mediumTypes[$medium['medium_type']]) {
                
                /**
                 * Default: display as links. 
                 */
                default:
                    $a = ViewHtmlElement::elementInit('a', $medium['key'], 'src', $this->getResource() . '/' . $medium['source']);
                    $a->setAttribute('target', '_blank');
                    $this->addInnerHtml($a);
                    break;
                    
                /**
                 * Images. 
                 */
                case 'image':
                    $width = IMAGE_SIZE < $medium['width'] ? IMAGE_SIZE : $medium['width'];
                    $img = ViewImg::imgInit($this->getResource() . '/' . $medium['source'], $medium['key'], $width);
                    $this->addInnerHtml($img);
                    break;
            }
        }
        
        /**
         * Page content. 
         */
        if (isset($this->fields['panel']['article']['content_text']) && $this->fields['panel']['article']['content_text'] != '') {
            $this->addP($this->fields['panel']['article']['content_text']);
        }
        
    }

}